package com.web.homework;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.web.DataSourceUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

@WebServlet("/work2")
public class work2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置请求编码
        request.setCharacterEncoding("UTF-8");
        // 设置响应内容类型和编码
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");

        // 获取关键字
        String keyword = request.getParameter("name");

        // 搜索学生信息
        List<String> students = searchStudents(keyword);

        // 打印所有学生信息到控制台
        for (String student : students) {
            System.out.println(student);
        }
    }

    // 搜索学生信息的方法
    private List<String> searchStudents(String keyword) {
        List<String> students = new ArrayList<>();
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        DataSource dataSource = null;

        Properties properties = new Properties();
        try {
            // 读配置文件
            properties.load(DataSourceUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
            // 初始化数据源
            dataSource = DruidDataSourceFactory.createDataSource(properties);

            // 获取连接
            connection = dataSource.getConnection();
            // 创建预编译语句
            String sql = "SELECT name FROM student WHERE name LIKE ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, "%" + keyword + "%");
            // 执行查询
            resultSet = preparedStatement.executeQuery();

            // 处理结果集
            while (resultSet.next()) {
                students.add(resultSet.getString("name"));
            }
        } catch (IOException | SQLException e) {
            throw new RuntimeException("初始化数据源失败: " + e.getMessage(), e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            // 关闭资源
            try {
                if (resultSet != null) resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (preparedStatement != null) preparedStatement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (connection != null) connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return students;
    }
}